package com.atguigu.ssyx.sys.service.impl;

import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.model.sys.RegionWare;
import com.atguigu.ssyx.sys.mapper.RegionWareMapper;
import com.atguigu.ssyx.sys.service.IRegionWareService;
import com.atguigu.ssyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;

/**
 * <p>
 * 城市仓库关联表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-22
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements IRegionWareService {
    @Autowired
    private RegionWareMapper regionWareMapper;

    @Override
    public IPage<RegionWare> selectPage(Page<RegionWare> pageParam, RegionWareQueryVo regionWareQueryVo) {
//         1.查询条件值是否为空
        String keyword = regionWareQueryVo.getKeyword();

        LambdaQueryWrapper<RegionWare> lambdaQuery = new LambdaQueryWrapper<>();

//        where   like y or  k= v
        if (!StringUtils.isEmpty(keyword)) {
            lambdaQuery.like(RegionWare::getRegionName, keyword).or().like(RegionWare::getWareName, keyword);
        }

//        2.构建where条件.
        Page<RegionWare> regionWarePage = baseMapper.selectPage(pageParam, lambdaQuery);

//         3.条件分页查询

//        4.数据返回
        return regionWarePage;
    }

    @Override
    public void saveRegionWare(RegionWare regionWare) {
//        查看该地区是否已经开通了
        LambdaQueryWrapper<RegionWare> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(RegionWare::getRegionId, regionWare.getRegionId()).eq(RegionWare::getWareId, regionWare.getWareId());
        Long aLong = regionWareMapper.selectCount(queryWrapper);
        if (aLong > 0) {
            throw new SsyxException(ResultCodeEnum.REGION_OPEN);
        }
//        没有开通就是插入数据
        baseMapper.insert(regionWare);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        RegionWare regionWare = baseMapper.selectById(id);
        regionWare.setStatus(status);
        baseMapper.updateById(regionWare);
    }


}
